package com.lucenten.collect.gnss.entity;

import cn.quevo.common.lang.DateFormatUtils;
import cn.quevo.common.lang.NumberUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StecpTeclTrans implements Serializable {

    /** 时间 */
    private LocalDateTime localDateTime;
    /** 卫星号 */
    private Integer prn;
    /** 仰角 */
    private Double azi;
    /** 斜角 */
    private Double ele;
    /** TECP */
    private Double tecp;
    /** TECL */
    private Double tecl;
    /** L1 */
    private Double l1;
    /** L2 */
    private Double l2;
    /** STEC */
    private Double stec;
    /** VTEC */
    private Double vtec;

    // 台站X
    private Double x;
    // 台站Y
    private Double y;
    // 台站Z
    private Double z;
    // 卫星X
    private Double xk;
    // 卫星Y
    private Double yk;
    // 卫星Z
    private Double zk;

    // JOIN字符串
    private String join;

    public String getJoin(){
        StringBuilder str = new StringBuilder("  ");
        str.append(this.format(localDateTime));
        this.formatLength(this.prn.toString(), 3, str);
        this.formatLength(this.azi, 7, str);
        this.formatLength(this.ele, 7, str);
        this.formatLength(this.tecp, 7, str);
        this.formatLength(this.tecl, 7, str);
        this.formatLength(this.stec, 7, str);
        this.formatLength(this.l1.toString(), 15, str);
        this.formatLength(this.l2.toString(), 15, str);
        this.formatLength(this.vtec, 7, str);

        this.formatLength(this.x.toString(), 7, str);
        this.formatLength(this.y.toString(), 7, str);
        this.formatLength(this.z.toString(), 7, str);

        this.formatLength(this.xk.toString(), 7, str);
        this.formatLength(this.yk.toString(), 7, str);
        this.formatLength(this.zk.toString(), 7, str);
        return str.toString();
    }

    private String format(LocalDateTime localDateTime) {
        return DateFormatUtils.format(localDateTime.toInstant(ZoneOffset.UTC),"yyyy MM dd HH mm ss");
    }
    /**
     * 格式化数字
     *
     * @param val
     * @return
     */
    private String format(Double val) {
        return String.format("%.2f", val);
    }

    /**
     * 格式化长度,不足用空格补全
     *
     * @param val
     * @param length
     * @param str
     */
    private void formatLength(String val, int length,StringBuilder str) {
        str.append("   ");
        if (val.length() == length) {
            str.append(val.trim());
            return ;
        }
        if (val.length() > length) {
            str.append(val.substring(0,length));
            return;
        }
        int qty = length - val.length();
        for (int i = 0; i < qty; i ++) {
            str.append(" ");
        }
        str.append(val);
    }

    /**
     * 格式化长度,不足用空格补全
     *
     * @param val
     * @param length
     * @param str
     */
    private void formatLength(Double val, int length,StringBuilder str) {
        this.formatLength(this.format(val), length, str);
    }


    public boolean equals(Object object) {
        if (!(object instanceof StecpTeclTrans)) {
            return false;
        }
        StecpTeclTrans teclTrans = (StecpTeclTrans) object;
        return teclTrans.getPrn().equals(teclTrans.getPrn()) && this.format(localDateTime).equalsIgnoreCase(this.format(teclTrans.getLocalDateTime()));
    }


    public int hashCode() {
        int code = this.prn.hashCode();
        code = 31 * code + localDateTime.hashCode();
        return code;
    }

    public static StecpTeclTrans builderLine(String line) {
        String[] args = line.split(",");
        // UTC 时间
        LocalDateTime localDateTime = LocalDateTime.ofInstant(DateFormatUtils.parse(args[0],"yyyy-MM-dd HH:mm:ss").toInstant(), ZoneId.of("UTC"));
        StecpTeclTrans teclTrans = StecpTeclTrans.builder().localDateTime(localDateTime).prn(NumberUtils.toInt(args[1].trim()))
                .azi(NumberUtils.toDouble(args[2].trim())).ele(NumberUtils.toDouble(args[3].trim()))
                .tecp(NumberUtils.toDouble(args[4].trim())).tecl(NumberUtils.toDouble(args[5].trim()))
                .l1(NumberUtils.toDouble(args[6].trim())).l2(NumberUtils.toDouble(args[7].trim()))
                .stec(0d).vtec(0d)
                .x(NumberUtils.toDouble(args[8])).y(NumberUtils.toDouble(args[9])).z(NumberUtils.toDouble(args[10]))
                .xk(NumberUtils.toDouble(args[11])).yk(NumberUtils.toDouble(args[12])).zk(NumberUtils.toDouble(args[13])).build();
        return teclTrans;
    }
}
